// AboutDlg.cpp : 实现文件
//
#include "basetsd.h"
#include "stdafx.h"
#include "wifishare.h"
#include "AboutDlg.h"
#include "afxdialogex.h"
#include "wifishareKernel.h"
#include "cpl.h"

// CAboutDlg 对话框

IMPLEMENT_DYNAMIC(CAboutDlg, CDialogEx)

CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_ABOUT_DIALOG, pParent)
{

}

CAboutDlg::~CAboutDlg()
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON1, m_Button_Restart);
	DDX_Control(pDX, IDC_EDIT2, m_EditBox_Link);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON1, &CAboutDlg::OnBnClickedButton1)
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	ON_WM_MOUSEWHEEL()
	ON_BN_CLICKED(IDC_BUTTON2, &CAboutDlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CAboutDlg 消息处理程序


BOOL CAboutDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	ChangeState_RestartButton(0);
	m_EditBox_Link.SetWindowTextW(L"2021 fix by 2546hhw    https://gitee.com/h2546/wifishare");

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CAboutDlg::OnPaint()
{
					   // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CDialogEx::OnPaint()
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

HCURSOR CAboutDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CAboutDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


UINT CAboutDlg::RestartICSService(LPVOID p)
{
	CAboutDlg *dlg = (CAboutDlg*)p;
	int ret;

	ret = lqx::ChangeServiceStatusW(L"SharedAccess", false, 25);
	if (ret == ERROR_SERVICE_REQUEST_TIMEOUT) {
		AfxMessageBox(L"操作超时。");
		dlg->ChangeState_RestartButton(0);
		return 0;
	}
	else if (ret != ERROR_SUCCESS && ret != ERROR_SERVICE_NOT_ACTIVE) {
		AfxMessageBox(L"操作失败。");
		dlg->ChangeState_RestartButton(0);
		return 0;
	}

	dlg->ChangeState_RestartButton(2);
	ret = lqx::ChangeServiceStatusW(L"SharedAccess", true, 25);
	if (ret == ERROR_SERVICE_REQUEST_TIMEOUT) {
		AfxMessageBox(L"操作超时。");
		dlg->ChangeState_RestartButton(0);
		return 0;
	}
	else if (ret != ERROR_SUCCESS ) {
		AfxMessageBox(L"操作失败。");
		dlg->ChangeState_RestartButton(0);
		return 0;
	}

	dlg->ChangeState_RestartButton(0);
	return 0;

}

void CAboutDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	ChangeState_RestartButton(1);
	AfxBeginThread(&RestartICSService, this);

}



//0：显示“重新启动ICS服务”  1：显示“正在停止ICS服务...”  2：显示“正在启动ICS服务...” 
void CAboutDlg::ChangeState_RestartButton(int state)
{

	switch (state)
	{
	case 0:
		m_Button_Restart.SetWindowTextW(L"重新启动ICS服务");
		m_Button_Restart.EnableWindow(TRUE);
		m_State_RestartButton = state;
		break;
	case 1:
		m_Button_Restart.SetWindowTextW(L"正在停止ICS服务...");
		m_Button_Restart.EnableWindow(FALSE);
		m_State_RestartButton = state;
		break;
	case 2:
		m_Button_Restart.SetWindowTextW(L"正在启动ICS服务...");
		m_Button_Restart.EnableWindow(FALSE);
		m_State_RestartButton = state;
		break;

	default:
		break;
	}
}

void CAboutDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
		SCROLLINFO scrollinfo;

	GetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

	switch (nSBCode)

	{

	case SB_BOTTOM:

		ScrollWindow(0, (scrollinfo.nPos - scrollinfo.nMax) * 10);

		scrollinfo.nPos = scrollinfo.nMax;

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		break;

	case SB_TOP:

		ScrollWindow(0, (scrollinfo.nPos - scrollinfo.nMin) * 10);

		scrollinfo.nPos = scrollinfo.nMin;

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		break;

	case SB_LINEUP:

		scrollinfo.nPos -= 1;

		if (scrollinfo.nPos < scrollinfo.nMin)

		{

			scrollinfo.nPos = scrollinfo.nMin;

			break;

		}

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		ScrollWindow(0, 10);

		break;

	case SB_LINEDOWN:

		scrollinfo.nPos += 1;

		if (scrollinfo.nPos > scrollinfo.nMax)

		{

			scrollinfo.nPos = scrollinfo.nMax;

			break;

		}

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		ScrollWindow(0, -10);

		break;

	case SB_PAGEUP:

		scrollinfo.nPos -= 5;

		if (scrollinfo.nPos < scrollinfo.nMin)

		{

			scrollinfo.nPos = scrollinfo.nMin;

			break;

		}

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		ScrollWindow(0, 10 * 5);

		break;

	case SB_PAGEDOWN:

		scrollinfo.nPos += 5;

		if (scrollinfo.nPos > scrollinfo.nMax)

		{

			scrollinfo.nPos = scrollinfo.nMax;

			break;

		}

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		ScrollWindow(0, -10 * 5);

		break;

	case SB_ENDSCROLL:

		// MessageBox("SB_ENDSCROLL");

		break;



	case SB_THUMBTRACK:

		ScrollWindow(0, (scrollinfo.nPos - nPos) * 10);

		scrollinfo.nPos = nPos;

		SetScrollInfo(SB_VERT, &scrollinfo, SIF_ALL);

		break;

	}


	CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}


void CAboutDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	SCROLLINFO scrollinfo;

	GetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

	switch (nSBCode)

	{

	case SB_RIGHT:

		ScrollWindow((scrollinfo.nMax - scrollinfo.nPos ) * 10,0 );

		scrollinfo.nPos = scrollinfo.nMax / 2;

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		break;

	case SB_LEFT:

		ScrollWindow((scrollinfo.nPos - scrollinfo.nMin) * 10,0 );

		scrollinfo.nPos = scrollinfo.nMin;

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		break;

	case SB_LINELEFT:

		scrollinfo.nPos -= 1; 

		if (scrollinfo.nPos < scrollinfo.nMin)

		{

			scrollinfo.nPos = scrollinfo.nMin;

			break;

		}

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		ScrollWindow(10, 0);

		break;

	case SB_LINERIGHT:

		scrollinfo.nPos += 1;

		if (scrollinfo.nPos > scrollinfo.nMax)

		{

			scrollinfo.nPos = scrollinfo.nMax;

			break;

		}

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		ScrollWindow(-10, 0);

		break;

	case SB_PAGELEFT:

		scrollinfo.nPos -= 5;

		if (scrollinfo.nPos < scrollinfo.nMin)

		{

			scrollinfo.nPos = scrollinfo.nMin;

			break;

		}

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		ScrollWindow(10 * 5, 0);

		break;

	case SB_PAGERIGHT:

		scrollinfo.nPos += 5;

		if (scrollinfo.nPos > scrollinfo.nMax)

		{

			scrollinfo.nPos = scrollinfo.nMax;

			break;

		}

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		ScrollWindow(-10 * 5, 0);

		break;

	case SB_ENDSCROLL:

		// MessageBox("SB_ENDSCROLL");

		break;

	

	case SB_THUMBTRACK:

		ScrollWindow( (scrollinfo.nPos - nPos) * 10,0);

		scrollinfo.nPos = nPos;

		SetScrollInfo(SB_HORZ, &scrollinfo, SIF_ALL);

		break;

	}

	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}


BOOL CAboutDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
		
	if(zDelta>0) 
		SendMessage(WM_VSCROLL, MAKEWPARAM( SB_LINEUP , 0), 0);
	
	else         
		SendMessage(WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), 0);

	return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);
}


void CAboutDlg::OnBnClickedButton2()
{
     //打开网络连接
	 
	HINSTANCE a = ShellExecute(this->m_hWnd, L"open",  L"control.exe" , L"netconnections", 0, SW_SHOW);
 
	 long int c ; 
	 c=PtrToUlong(a); // 将a指针转换成 无符号长型
	 switch (c)

	 {

	 case 0:

		 MessageBox(L"The operating system is out of memory or resources.内存不足或资源不足", L"wifishare2021", 0);

	 case ERROR_FILE_NOT_FOUND:

			 MessageBox(L"control.exe not found , 控制面板程序没有找到。", L"wifishare2021", 0);

	 case ERROR_PATH_NOT_FOUND:

				 MessageBox(L"control.exe path not found, 控制面板程序的路径没有设置。", L"wifishare2021", 0);

	 }
	 
} 